$(function () {
    $("#fileJqGrid").jqGrid({
        url: baseURL + 'sys/oss/list',
        datatype: "json",
        colModel: [
            {label: 'id', name: 'id', width: 20, key: true},
            {label: '文件名称', name: 'fileName', width: 60},
            {label: '文件类型', name: 'fileType', width: 40},
            {label: '文件大小', name: 'fileSize', width: 40},
            {label: '文件后缀', name: 'suffix', width: 40},
            /*{label: 'URL地址', name: 'url', width: 160},*/
            {
                label: 'URL地址', name: 'url', width: 45, formatter: function (value, options, row) {
                    return row.fileType === "image" ?
                        '<img src="' + value + '" alt="' + row.fileName + '" width="42"/>' :
                        '<a href="' + value + '">' + row.fileName + '</a>';
                }
            },
            { name: 'url', width: 120, hidden: true },
            {label: '创建时间', name: 'createDate', width: 60}
        ],
        viewrecords: true,
        height: 385,
        rowNum: 10,
        rowList: [10, 30, 50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth: true,
        multiselect: true,
        pager: "#fileJqGridPager",
        jsonReader: {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames: {
            page: "page",
            rows: "limit",
            order: "order"
        },
        gridComplete: function () {
            //隐藏grid底部滚动条
            $("#fileJqGrid").closest(".ui-jqgrid-bdiv").css({"overflow-x": "hidden"});
        }
    });
    $("#fileJqGrid").jqGrid('setGridWidth', 980);
    $(window).resize();
    // $("#fieLayer .ui-jqgrid").css("width", "980px");
    // $("#fieLayer .ui-jqgrid-view").css("width", "980px");
    // $("#fieLayer .ui-jqgrid-hdiv").css("width", "978px");
    // $("#fieLayer .ui-jqgrid-bdiv").css("width", "978px");

    new AjaxUpload('#upload', {
        action: baseURL + "sys/oss/upload",
        name: 'file',
        autoSubmit: true,
        responseType: "json",
        onSubmit: function (file, extension) {
            /*if(vm.config.type == null){
                alert("云存储配置未配置");
                return false;
            }
            if (!(extension && /^(jpg|jpeg|png|gif)$/.test(extension.toLowerCase()))){
                alert('只支持jpg、png、gif格式的图片！');
                return false;
            }*/
        },
        onComplete: function (file, r) {
            if (r.code == 0) {
                alert(r.url);
                fileVue.reload();
            } else {
                alert(r.msg);
            }
        }
    });

});

var fileVue = new Vue({
    el: '#fieLayer',
    data: {
        search: {
            fileName: null
        }
    },
    methods: {
        query: function () {
            fileVue.reload();
        },
        reload: function () {
            var page = $("#fileJqGrid").jqGrid('getGridParam', 'page');
            $("#fileJqGrid").jqGrid('setGridParam', {
                postData: {'fileJqGrid': fileVue.search.fileJqGrid},
                page: page
            }).trigger("reloadGrid");
        },
        fileSelect: function (param, multiSelect) {
            layer.open({
                type: 1,
                offset: '50px',
                skin: 'layui-layer-molv',
                title: "选择文件",
                area: ['1000px', '650px'],
                shade: 0,
                shadeClose: false,
                content: jQuery("#fieLayer"),
                btn: ['确定', '取消'],
                success: function () {
                    $(window).resize();
                },
                btn1: function (index) {
                    var params = param.split('.');
                    var filePath;
                    if (multiSelect) {
                        filePath = selectedRows();
                    } else {
                        filePath = selectedRow();
                    }
                    vm[params[0]][params[1]] = filePath;
                    layer.close(index);
                }
            });
        },
    }
});


//选择一条记录
function selectedRow() {
    var grid = $("#fileJqGrid");
    var rowKey = grid.getGridParam("selrow");
    if (!rowKey) {
        alert("请选择一条文件");
        return;
    }

    var selectedIDs = grid.getGridParam("selarrrow");
    if (selectedIDs.length > 1) {
        alert("只能选择一条记录");
        return;
    }
    var rowData = grid.getRowData(selectedIDs[0]);
    return rowData.url;
}

//选择多条条记录
function selectedRows() {
    var grid = $("#fileJqGrid");
    var rowKey = grid.getGridParam("selrow");
    if (!rowKey) {
        alert("请选择文件");
        return;
    }

    var urls = "";
    var selectedIDs = grid.getGridParam("selarrrow");
    for (var i = 0, len = selectedIDs.length; i < len; i++) {
        var url = grid.getRowData(selectedIDs[i]).url;
        urls += url;
        if (i < len - 1) {
            urls += ","
        }
    }

    return urls;
}